package com.jy.rhin.domain.registration;

import com.jy.api.misc.MiscAPI;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.WSSYBMXTDaiMaDto;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.domain.audit.entity.AuditInfo;
import com.jy.rhin.domain.registration.entity.CodeEnum;
import com.jy.rhin.domain.registration.entity.MiscZhiYu;
import com.jy.rhin.sdk.dto.audit.AuditWrapperDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;

/**
 * @author MaYuXuan
 * @date 2022年6月21日 08:45:19
 * @Description 值集服务
 */
@Service
public class InfraValueSetService {

    @Resource
    MiscAPI miscApi;

    private final Map<Function<AuditWrapperDto, Boolean>, String> map = new HashMap<>();

    @PostConstruct
    public void init() {
        map.put(auditWrapperDto -> checkCode(auditWrapperDto.getEventInfo().getCaoZuoLx(), CodeEnum.EVENT_ACTION_CODE), "操作类型");
        map.put(auditWrapperDto -> checkCode(auditWrapperDto.getEventInfo().getShiJianJg(), CodeEnum.EVENT_OUTCOME_INDICATOR), "事件结果");
        map.put(auditWrapperDto -> checkCode(auditWrapperDto.getEventInfo().getShiJianBs()), "事件标识");
        map.put(auditWrapperDto -> checkCode(auditWrapperDto.getEventInfo().getShiJianLx()), "事件类型");
        map.put(auditWrapperDto -> auditWrapperDto.getParticipantList().stream().anyMatch(auditParticipantDto -> checkCode(auditParticipantDto.getJueSe())), "参与者角色");
    }

    /**
     * 通过标识符获取值集列表
     *
     * @param zhiyuCode
     * @return
     */
    private List<MiscZhiYu> getJiZhiByBiaoShiFu(String zhiyuCode) {
        Result<WSSYBMXTDaiMaDto> dtoResult = miscApi.jiZhiHQ(zhiyuCode);
        if(!CollectionUtils.isEmpty(dtoResult.getData().getDaiMaVoList())){
            List<MiscZhiYu> result = new ArrayList<>();
            for (DaiMaDto daiMaVo : dtoResult.getData().getDaiMaVoList()) {
                result.add(new MiscZhiYu(daiMaVo.getCode(), daiMaVo.getValue()));
            }
            return result;
        }
        return Collections.emptyList();
    }

    /**
     * 校验值域信息
     *
     * @param dto
     */
    public AuditInfo checkValueField(AuditWrapperDto dto) {
        AuditInfo auditInfo = new AuditInfo();
        auditInfo.setCheckStatus(true);
        auditInfo.setFailureReason("");
        Iterator<Map.Entry<Function<AuditWrapperDto, Boolean>, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Function<AuditWrapperDto, Boolean>, String> next = iterator.next();
            if (Boolean.FALSE.equals(next.getKey().apply(dto))) {
                auditInfo.setCheckStatus(false);
                auditInfo.setFailureReason(next.getValue() + " 值域错误");
                break;
            }
        }
        return auditInfo;
    }

    private boolean checkCode(CodedValueTypeDto value) {
        String codeSystem = value.getCodeSystem();
        CodeEnum lookup = CodeEnum.lookup(codeSystem);
        return checkCode(value.getCode(), lookup);
    }

    private boolean checkCode(String value, CodeEnum codeEnum) {
        if (codeEnum == null) {
            return false;
        }
        List<MiscZhiYu> zhiYu = getJiZhiByBiaoShiFu(codeEnum.getId());
        return zhiYu.stream().anyMatch(miscZhiYu -> StringUtils.equals(value, miscZhiYu.getCode()));
    }

}
